using Senparc.Weixin.Entities;
using Senparc.Weixin.Exceptions;
using Senparc.Weixin.MP.Helpers;
using System;
using System.Xml.Linq;
namespace Senparc.Weixin.MP.Entities
{
	public class ResponseMessageBase : Senparc.Weixin.Entities.ResponseMessageBase, IResponseMessageBase, Senparc.Weixin.Entities.IResponseMessageBase, IMessageBase
	{
		public virtual ResponseMsgType MsgType
		{
			get
			{
				return ResponseMsgType.Text;
			}
		}
		[Obsolete("建议使用CreateFromRequestMessage<T>(IRequestMessageBase requestMessage)取代此方法")]
		public static ResponseMessageBase CreateFromRequestMessage(IRequestMessageBase requestMessage, ResponseMsgType msgType)
		{
			ResponseMessageBase responseMessageBase = null;
			try
			{
				switch (msgType)
				{
				case ResponseMsgType.Text:
					responseMessageBase = new ResponseMessageText();
					break;
				case ResponseMsgType.News:
					responseMessageBase = new ResponseMessageNews();
					break;
				case ResponseMsgType.Music:
					responseMessageBase = new ResponseMessageMusic();
					break;
				case ResponseMsgType.Image:
					responseMessageBase = new ResponseMessageImage();
					break;
				case ResponseMsgType.Voice:
					responseMessageBase = new ResponseMessageVoice();
					break;
				case ResponseMsgType.Video:
					responseMessageBase = new ResponseMessageVideo();
					break;
				case ResponseMsgType.Transfer_Customer_Service:
					responseMessageBase = new ResponseMessageTransfer_Customer_Service();
					break;
				default:
					throw new UnknownRequestMsgTypeException(string.Format("ResponseMsgType没有为 {0} 提供对应处理程序。", msgType), new ArgumentOutOfRangeException());
				}
				responseMessageBase.ToUserName = requestMessage.FromUserName;
				responseMessageBase.FromUserName = requestMessage.ToUserName;
				responseMessageBase.CreateTime = DateTime.Now;
			}
			catch (Exception inner)
			{
				throw new WeixinException("CreateFromRequestMessage过程发生异常", inner);
			}
			return responseMessageBase;
		}
		public static T CreateFromRequestMessage<T>(IRequestMessageBase requestMessage) where T : ResponseMessageBase
		{
			T result;
			try
			{
				Type typeFromHandle = typeof(T);
				string value = typeFromHandle.Name.Replace("ResponseMessage", "");
				ResponseMsgType msgType = (ResponseMsgType)Enum.Parse(typeof(ResponseMsgType), value);
				result = (ResponseMessageBase.CreateFromRequestMessage(requestMessage, msgType) as T);
			}
			catch (Exception inner)
			{
				throw new WeixinException("ResponseMessageBase.CreateFromRequestMessage<T>过程发生异常！", inner);
			}
			return result;
		}
		public static IResponseMessageBase CreateFromResponseXml(string xml)
		{
			IResponseMessageBase result;
			try
			{
				if (string.IsNullOrEmpty(xml))
				{
					result = null;
				}
				else
				{
					XDocument xDocument = XDocument.Parse(xml);
					ResponseMessageBase responseMessageBase = null;
					switch ((ResponseMsgType)Enum.Parse(typeof(ResponseMsgType), xDocument.Root.Element("MsgType").Value, true))
					{
					case ResponseMsgType.Text:
						responseMessageBase = new ResponseMessageText();
						break;
					case ResponseMsgType.News:
						responseMessageBase = new ResponseMessageNews();
						break;
					case ResponseMsgType.Music:
						responseMessageBase = new ResponseMessageMusic();
						break;
					case ResponseMsgType.Image:
						responseMessageBase = new ResponseMessageImage();
						break;
					case ResponseMsgType.Voice:
						responseMessageBase = new ResponseMessageVoice();
						break;
					case ResponseMsgType.Video:
						responseMessageBase = new ResponseMessageVideo();
						break;
					case ResponseMsgType.Transfer_Customer_Service:
						responseMessageBase = new ResponseMessageTransfer_Customer_Service();
						break;
					}
					//responseMessageBase.FillEntityWithXml(xDocument);
					result = responseMessageBase;
				}
			}
			catch (Exception ex)
			{
				throw new WeixinException("ResponseMessageBase.CreateFromResponseXml<T>过程发生异常！" + ex.Message, ex);
			}
			return result;
		}
	}
}
